package com.kye.express.business.welfare_integral.viewbinder;

import android.databinding.DataBindingUtil;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.jakewharton.rxbinding2.view.RxView;
import com.kye.base.DebounceObservableTransformer;
import com.kye.express.R;
import com.kye.express.business.welfare_integral.tab.IntegralTabEntity;
import com.kye.express.databinding.ItemIntegralCenterListRecordStatusBinding;
import com.kye.model.bean.integral.IntegralRecordStatusBean;
import com.kye.model.bean.integral.IntegralRecordType;

import java.util.ArrayList;

import io.reactivex.functions.Consumer;
import me.drakeet.multitype.ItemViewBinder;

/**
 * @author : Joe
 * @version : 1.0
 * @editor : Joe
 * @created : 12/01/2018
 * @updated : 12/01/2018
 * @description : <Description>
 * @update_reason : <UpdateReason>
 */

public class IntegralTabItemViewBinder extends ItemViewBinder<IntegralRecordStatusBean, IntegralTabItemViewBinder.ViewHolder> {

    private OnTabLayoutListenr onTabLayoutListenr;

    public IntegralTabItemViewBinder(OnTabLayoutListenr onTabLayoutListenr) {
        this.onTabLayoutListenr = onTabLayoutListenr;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View view = inflater.inflate(R.layout.item_integral_center_list_record_status, parent, false);
        return new ViewHolder(view);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull IntegralRecordStatusBean item) {
        holder.bindData(item);
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements OnTabSelectListener {

        private IntegralRecordStatusBean item;
        private ItemIntegralCenterListRecordStatusBinding binding;
        private ArrayList<CustomTabEntity> tabs = new ArrayList<>();

        public ViewHolder(final View itemView) {
            super(itemView);
            binding = DataBindingUtil.bind(itemView);
            tabs = new ArrayList<>();
            tabs.add(new IntegralTabEntity("兑现记录", 0, 0));
            tabs.add(new IntegralTabEntity("获取记录", 0, 0));
            binding.tabLayout.setTabData(tabs);
            binding.tabLayout.setOnTabSelectListener(this);
            RxView.clicks(binding.dateFrom)
                    .compose(new DebounceObservableTransformer<Object>())
                    .subscribe(new Consumer<Object>() {
                        @Override
                        public void accept(Object o) throws Exception {
                            if (onTabLayoutListenr != null) {
                                onTabLayoutListenr.onSelectDateFrom(item);
                            }
                        }
                    });
            RxView.clicks(binding.dateTo)
                    .compose(new DebounceObservableTransformer<Object>())
                    .subscribe(new Consumer<Object>() {
                        @Override
                        public void accept(Object o) throws Exception {
                            if (onTabLayoutListenr != null) {
                                onTabLayoutListenr.onSelectDateTo(item);
                            }
                        }
                    });
            // todo : @Deprecated
            RxView.clicks(binding.query)
                    .compose(new DebounceObservableTransformer<Object>())
                    .subscribe(new Consumer<Object>() {
                        @Override
                        public void accept(Object o) throws Exception {
                            if (onTabLayoutListenr != null) {
                                onTabLayoutListenr.onQueryIntegralObtainData(item);
                            }
                        }
                    });
            // todo : update
            RxView.clicks(binding.queryNew)
                    .compose(new DebounceObservableTransformer<Object>())
                    .subscribe(new Consumer<Object>() {
                        @Override
                        public void accept(Object o) throws Exception {
                            if (onTabLayoutListenr != null) {
                                onTabLayoutListenr.onSelectDate(item);
                            }
                        }
                    });
        }

        public void bindData(IntegralRecordStatusBean item) {
            this.item = item;
            if (item.recordType == IntegralRecordType.TYPE_EXCHANGE) {
                binding.tabLayout.setCurrentTab(0);
            } else {
                binding.tabLayout.setCurrentTab(1);
            }
            binding.setData(item);
        }

        @Override
        public void onTabSelect(int position) {
            item.setRecordType(position == 0 ? IntegralRecordType.TYPE_EXCHANGE : IntegralRecordType.TYPE_OBTAIN);
            binding.setData(item);
            if (onTabLayoutListenr != null) {
                onTabLayoutListenr.onTabSelect(position, item);
            }
        }

        @Override
        public void onTabReselect(int position) {
            if (onTabLayoutListenr != null) {
                onTabLayoutListenr.onTabReselect(position, item);
            }
        }
    }

    public interface OnTabLayoutListenr {
        void onTabSelect(int position, IntegralRecordStatusBean item);
        void onTabReselect(int position, IntegralRecordStatusBean item);
        @Deprecated
        void onSelectDateFrom(IntegralRecordStatusBean item);
        @Deprecated
        void onSelectDateTo(IntegralRecordStatusBean item);
        @Deprecated
        void onQueryIntegralObtainData(IntegralRecordStatusBean item);

        void onSelectDate(IntegralRecordStatusBean item);
    }
}
